#!/usr/bin/perl

##HACKABOT_HELP##
# Fire people!
# !tardhat [someone | --value someone | --low | --high | --nicks]
##HACKABOT_HELP##

use strict;
use DBI;
use XML::Simple;

my $CONFIG = XMLin($ENV{'HACKABOT_CFG'});
my $DBCFG = $CONFIG->{'cmdconfig'}->{'database'};

my $dbhost = $DBCFG->{'host'};
my $dbname = $DBCFG->{'name'};
my $dbuser = $DBCFG->{'user'};
my $dbpass = $DBCFG->{'pass'};

sub add_tard {
	my $name = shift;
	my $nick = shift;
	my $chan = shift;
	my ($dbh, $sth);
	
	my $val = 1;

	$dbh = DBI->connect("DBI:mysql:$dbname:$dbhost", $dbuser, $dbpass, { PrintError => 0 });
	if (!$dbh) {
		die "tard: Failed to connect to database\n";
	}
	
	my $time = `date "+%F %R"`;
	chomp $time;

	$nick = $dbh->quote($nick);
	$chan = $dbh->quote($chan);
	$time = $dbh->quote($time);
	$name = $dbh->quote($name);
	
	$dbh->do("UPDATE tard SET tard = 0 WHERE tard = 1") or
	die "tard: DB stuff failed.\n";

	$dbh->do("INSERT tard SET date = $time, name = $name, value = (value + $val), tard = 1, chan = $chan, nick = $nick") or
	$dbh->do("UPDATE tard SET date = $time, value = (value + $val), chan = $chan, tard = 1, nick = $nick WHERE name = $name") or
	die "tard: DB stuff failed.\n";
	
	$dbh->disconnect;
}

sub get_score {
	my $name = shift;
	my ($dbh, $sth);

	$dbh = DBI->connect("DBI:mysql:$dbname:$dbhost", $dbuser, $dbpass, { PrintError => 0 });
	if (!$dbh) {
		die "tard: Failed to connect to database\n";
	}
	
	$name = $dbh->quote($name);
	
	$sth = $dbh->prepare("SELECT value FROM tard WHERE name = $name");
	$sth->execute;
	my $row = $sth->fetchrow_hashref;
	my $value = $row->{'value'};
	$sth->finish();

	$dbh->disconnect;

	if (not defined $value) {
		$value = 0;
	}

	return $value;
}

sub get_tard {
	my ($dbh, $sth);

	$dbh = DBI->connect("DBI:mysql:$dbname:$dbhost", $dbuser, $dbpass, { PrintError => 0 });
	if (!$dbh) {
		die "tard: Failed to connect to database\n";
	}
	
	$sth = $dbh->prepare("SELECT name FROM tard WHERE tard = 1 LIMIT 1");
	$sth->execute;
	my $row = $sth->fetchrow_hashref;
	my $value = $row->{'name'};
	$sth->finish();

	$dbh->disconnect;
	
	if (not defined $value) {
		$value = "NONE";
	}

	return $value;
}

sub get_list {
	my $order = shift;
	my $chan = shift;
	my ($dbh, $sth);
	
	$dbh = DBI->connect("DBI:mysql:$dbname:$dbhost", $dbuser, $dbpass, { PrintError => 1 });
	if (!$dbh) {
		die "tard: Failed to connect to database\n";
	}

	if ($order eq "low") {
		$order = "ASC";
	}
	elsif ($order eq "high") {
		$order = "DESC";
	}
	else {
		die "Unknown order $order\n";
	}

	my $where = "";
	if (defined $chan) {
		my $names = `echo 'names $chan' | $ENV{'HACKABOT_DIR'}/scripts/client`;
		chomp $names;
		$names =~ s/^\S+\s+\S+\s+//;
		$where = "WHERE 0";
		foreach $_ (split(/\s+/, $names)) {
			$_ = $dbh->quote($_);
			$where .= " OR name = $_";
		}
	}	

	$sth = $dbh->prepare("SELECT name, value FROM tard $where ORDER BY value $order LIMIT 3");
	$sth->execute;
	my @list;
	my $row;
	for (my $i = 0; $i < 3 and ($row = $sth->fetchrow_hashref); $i++) {
		$list[$i] = $row;
	}
	$sth->finish();

	$dbh->disconnect;

	return @list;
}

sub print_tard {
	my $tard = get_tard();
	my $tlen = length($tard);
	my $offset = $tlen / 2 - 3;
	my $offstr = "";
	my $offnik = "";
	if ($offset < 0) {
		for (my $i = $offset; $i < -1; $i++) {
			$offnik .= " ";
		}
	}
	else {	
		for (my $i = $offset; $i > 0; $i--) {
			$offstr .= " ";
		}
	}

	print $offstr."  /\\\n";
	print $offstr." /  \\\n";
	print $offstr."/TARD\\\n";
	print $offnik.$tard."\n";
}

my ($name, $nick, $chan, $type);
while (<>) {
	if (/^msg\s+(\S+.*)/) {
		$name = $1;
	}
	elsif (/^to\s+(#\S+)/) {
		$chan = $1;
	}
	elsif (/^nick\s+(\S+)/) {
		$nick = $1;
	}
	elsif (/^type\s+(\S+)/) {
		$type = $1;
	}
}

print "sendnext\n"; 
if (defined $name and not $name =~ /(-v|--value|-h|--high|-l|--low|-n|--nicks)/) {
	if ($type ne "pubmsg") {
		print "$nick: This isn't a channel! Don't be rude in secret!\n";
		exit;
	}
	if ($nick eq get_tard()) {
		print "$nick: Oh hush up, you are the tard!\n";
		exit;
	}
	
	$name =~ s/^(\S+)/$1/;
	add_tard($1, $nick, $chan);
	print_tard();
}
elsif (defined $name and $name =~ /(-h|--high|-l|--low|-n|--nicks)/) {
	my $order;
	my $nicks;
	
	$order = ($name =~ /(-l|--low)/)? "low" : "high";
	$nicks = ($name =~ /(-n|--nicks)/)? $chan : undef;
	
	my @list = get_list($order, $nicks);
	
	if ($order eq "high") {
		print "Top Tards:\n";
	}
	else {
		print "Little Tards:\n";
	}
	
	foreach $_ (@list) {
		print "  $_->{'value'} $_->{'name'}\n";
	}
}
elsif (defined $name and $name =~ /(-v|--value)/) {
	$name =~ s/(-v|--value)\s+(\S+)/$2/;
	my $val = get_score($name);
	if ($val == "") { $val = 0; }
	if ($val == "1") {
		print "$name has been a tard $val time!\n";
	}
	else {
		print "$name has been a tard $val times!\n";
	}
}
else {
	print_tard();
}
